const NEED_NEW_LINE_ELEMS = 'p, pre, h1, h2, h3, h4, h5, h6';

// 简书
if('www.jianshu.com' == window.location.host) {
	console.log("简书");

	// 复制文章内容到一个新的区域, 然后屏蔽原来的文章内容, 以消除copy事件绑定
	// var article = document.getElementsByTagName('article')[0];
	// article.setAttribute('style', "display: none;");
	// var _article = document.createElement('article');
	// _article.setAttribute('class', article.getAttribute("class"));
	// _article.innerHTML = article.innerHTML
	// article.innerHTML = ''
	// article.parentNode.insertBefore(_article, article);
	$(function() {
		var $article = $('article:first');
		var $_article = $('<article></article>');
		$_article.addClass($article.attr('class'));
		$_article.addClass('uid-jianshu-article');
		// console.log("$_article", $_article);
		$_article.html($article.html());

		// 删除空行
		$_article.find('p').each(function() {
			const $this = $(this);
			const thisText = $this.text().trim();
			const isTextEmpty = thisText == '';
			const $thisImg = $this.find('img')
			const isNotImg = $thisImg.length == 0;

			if(isTextEmpty && isNotImg) {
				$this.remove();
			}
		});

		// 添加换行, 云笔记本复制时每个段才有换行
		$_article.find(NEED_NEW_LINE_ELEMS).after('<br />');

		$article.before($_article);
		$article.remove();
		InsertSelectBtn('h1:eq(1)', 'article:first');

		// 慢加载的图片修复
		setTimeout(function() {
			// console.log('setTimeout');
			$('article').find('img').each(function() {
				// console.log('each img', this);
				$(this).attr('src', $(this).data('original-src')).removeClass('image-loading');
			});
			$('article').css('height', 'initial');
		}, 2000);
	});
}


// 复制CSDN时整理样式
if(-1 != window.location.host.indexOf("blog.csdn.net")) {
	console.log("复制CSDN时整理样式");

	// 删除登录弹窗 $('.passport-login-container')
	const loginContainerTimer = setInterval(function() {
		// console.log('查找并清除登录弹窗中');
		let $loginContainer = $('.passport-login-container');
		if($loginContainer.length) {
			$loginContainer.remove();
			clearInterval(loginContainerTimer);
			// console.log('已清除登录弹窗');
		}
	}, 100);

	// 复制的内容经常粘在一起, 本来想用加空行的方式解决问题, 发现并不是万能的, 会影响正常内容, 准备另想办法
	// $('article p').before("<p>&nbsp;</p>").after("<p>&nbsp;</p>");

	$("article :header").before("<p>&nbsp;</p>");
	$('article .pre-numbering').remove();
	$('#content_views pre, #content_views pre code').css("user-select", "auto");

	const contentFilter = () => {
		var $article = $('div.blog-content-box:first');
		var $_article = $('<div></div>');
		$_article.addClass($article.attr('class'));
		$_article.html($article.html());

		// 删除文章底部知识点提示栏
		$_article.find('#treeSkill').remove();

		// 删除空行
		$_article.find('p').each(function() {
			const $this = $(this);
			const thisText = $this.text().trim();
			const isTextEmpty = thisText == '';
			const $thisImg = $this.find('img')
			const isNotImg = $thisImg.length == 0;

			if(isTextEmpty && isNotImg) {
				$this.remove();
			}
		});

		// 添加换行, 云笔记本复制时每个段才有换行
		$_article.find(NEED_NEW_LINE_ELEMS).after('<br />');

		$article.before($_article);
		$article.remove();
		InsertSelectBtn('#articleContentId', 'article:first');

		// 模拟点未代码展开
		$('div.hide-preCode-box img').trigger("click");
	}

	// 两秒钟后处理文章内容, 免得等整个页面装载完要好久, 会有生成两个按钮的风险, 但效率更重要
	setTimeout(function() {
		contentFilter();
	}, 2000);

	// 内容部分复制一份, 替代原内容, 绕过原来内容的copy事件绑定
	$(function() {
		contentFilter();
	});
}

// 复制cnblogs时整理样式
if('www.cnblogs.com' == window.location.host) {
	console.log("复制cnblogs时整理样式");

	var $post = $('#cnblogs_post_body');
	if($post.length > 0) {
		InsertSelectBtn('#topics', $post);

		if($post.find('.cnblogs_Highlighter').length > 0) {
			// console.log("找到代码块");
			var findCodeTimer = setInterval(function() {
				// 代码段左边的编号区
				var $codeIds = $post.find('td.gutter');
				if(0 == $codeIds.length) {
					// console.log("代码块未加载完");
					return;
				}

				// console.log("代码块加载完成");
				clearInterval(findCodeTimer);
				$codeIds.remove();
				$post.find('img[src="//common.cnblogs.com/images/copycode.gif"]').remove()
			}, 200);
		}
	}
}

// 复制头条
if('www.toutiao.com' == window.location.host) {
	console.log("复制头条");

	var $articleContent = $('.article-content');
	if($articleContent.length > 0) {
		InsertSelectBtn('.article-content > h1:first', 'article.tt-article-content');
	}
}

// 阿里 iconfont 全选图标
if(-1 != window.location.href.indexOf("www.iconfont.cn/collections/detail")) {
	console.log("阿里 iconfont 全选图标, 抄自 https://github.com/jingyinggong/iconFontCollector, 感谢作者");

	var timeout = null;
	function init() {
		var bar = document.querySelector(".block-bar-right .block-radius-btn-group");
		if(!bar) {
			tryToDetect();
			return;
		} else {
			clearTimeout(timeout);
		}

		var btn = document.createElement('span');
		btn.className = 'radius-btn radius-btn-like';
		btn.title = "全选";
		btn.style.backgroundColor="mediumpurple";
		btn.innerHTML = `<svg class="icon" style="width: 0.6em; height: 0.6em;vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="35710"><path d="M102.4 302.08c5.12 5.12 15.36 10.24 25.6 10.24s15.36-5.12 25.6-10.24l174.08-174.08c5.12-5.12 10.24-15.36 10.24-25.6s-5.12-15.36-10.24-25.6c-20.48-5.12-40.96-5.12-51.2 10.24l-148.48 153.6-71.68-76.8C51.2 158.72 40.96 158.72 35.84 158.72c-10.24 0-15.36 5.12-25.6 10.24-5.12 5.12-10.24 10.24-10.24 20.48s5.12 15.36 10.24 25.6L102.4 302.08zM276.48 409.6l-148.48 153.6-71.68-71.68C51.2 486.4 40.96 481.28 35.84 481.28c-10.24 0-15.36 5.12-25.6 10.24-5.12 10.24-10.24 15.36-10.24 25.6s5.12 15.36 10.24 25.6L102.4 629.76c5.12 5.12 15.36 10.24 25.6 10.24s15.36-5.12 25.6-10.24L322.56 460.8c5.12-5.12 10.24-15.36 10.24-25.6s-5.12-15.36-10.24-25.6c-10.24-10.24-30.72-10.24-46.08 0z m0 327.68l-148.48 153.6L56.32 819.2c-5.12-5.12-15.36-10.24-25.6-10.24s-15.36 5.12-25.6 10.24c0 5.12-5.12 15.36-5.12 25.6s5.12 15.36 10.24 25.6L102.4 957.44c5.12 5.12 15.36 10.24 25.6 10.24h5.12c10.24 0 15.36-5.12 25.6-10.24l174.08-174.08c5.12-5.12 10.24-15.36 10.24-25.6s-5.12-15.36-10.24-25.6c-25.6-5.12-40.96-5.12-56.32 5.12zM1024 153.6c0 30.72-25.6 56.32-56.32 56.32h-460.8c-30.72 0-61.44-25.6-61.44-56.32 0-30.72 25.6-56.32 56.32-56.32h455.68c35.84-5.12 66.56 20.48 66.56 56.32z m0 358.4c0 30.72-25.6 56.32-56.32 56.32h-460.8c-30.72 0-56.32-25.6-56.32-56.32s25.6-56.32 56.32-56.32h455.68c35.84 0 61.44 25.6 61.44 56.32z m-5.12 358.4c0 30.72-25.6 56.32-56.32 56.32h-460.8c-30.72 0-56.32-25.6-56.32-56.32 0-30.72 25.6-56.32 56.32-56.32h455.68c35.84 0 61.44 25.6 61.44 56.32z m0 0" fill="#ffffff" p-id="LoganGongPlugin"></path></svg>`;
		btn.addEventListener('click', function() {
			document.querySelectorAll('.icon-gouwuche1').forEach(item=>item.click());
		}, false);

		bar.appendChild(btn);
	}

	function tryToDetect() {
		timeout = setTimeout(init, 1000);
	}

	tryToDetect();
}
